﻿Public Class frmDTR
    Private frmParent As frmMain
    Dim validator As New PropertyValidator
    Dim logMgr As New LogBiometricsManager
    Dim empInfoMgr As New EmployeeInfoManager
    Private Data As Employee
    Sub New(ByVal emp As Employee)
        InitializeComponent()
        Data = emp
    End Sub

    Private Sub frmDTR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Dim dataList As List(Of LogBiometrics) = logMgr.getLogsByEmployeeId(Data.id)
        populateListView(dataList)

        If Data.role = "1" Then
            grpAdminControls.Visible = True
        End If

    End Sub

    Public Sub populateListView(ByVal logList As List(Of LogBiometrics))
        ClearAllFields()
        Try
            For Each log As LogBiometrics In logList

                Dim empInfo As EmployeeInfo = New EmployeeInfoManager().retrieveEmployeeInfoByEmployeeId(log.employee_id)

                Dim item As ListViewItem
                item = lvwAttendanceLogs.Items.Add(empInfo.last_name.ToString() + "," + empInfo.first_name.ToString())
                item.SubItems.Add(empInfo.employee_no)
                item.SubItems.Add(log.time_log)
                item.SubItems.Add(If((log.log_type = "0"), "Time In", "Time Out"))
                item.SubItems.Add(log.id)
                item.SubItems.Add(log.biometrics_id)
                item.SubItems.Add(log.employee_id)
            Next
            grpAttendanceLogs.Text = "Total Record(s): " + lvwAttendanceLogs.Items.Count.ToString()
            validator.resizes(lvwAttendanceLogs)
        Catch ex As Exception
            MsgBox("Can't load Attendance Logs" & vbCrLf & ex.Message)
        End Try
    End Sub

    Public Function ClearAllFields()
        validator.ResetControls(grpAttendanceLogs)
        grpAttendanceLogs.Text = "Total Record(s): 0"
        btnDelete.Enabled = False
        validator.resizes(lvwAttendanceLogs)
        Clipboard.Clear()
        Return 0
    End Function

    Private Sub lvwAttendanceLogs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwAttendanceLogs.SelectedIndexChanged

        btnDelete.Enabled = (lvwAttendanceLogs.SelectedItems.Count > 0)

    End Sub

  
    Private Sub btnExecuteSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecuteSearch.Click
        Dim fromDate As String = dtpFrom.Value.ToString("yyyy-M-d HH:mm:ss")
        Dim toDate As String = dtpTo.Value.ToString("yyyy-M-d HH:mm:ss")

        Dim dataList As List(Of LogBiometrics) = logMgr.retrieveLogBioByDate(Data.id, fromDate, toDate)
        populateListView(dataList)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwAttendanceLogs.SelectedItems.Count
                logMgr.deleteLogBiometricsByID(lvwAttendanceLogs.SelectedItems(i - 1).SubItems(4).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmDTR_Load(vbNull, EventArgs.Empty)
            grpAttendanceLogs.Text = "Total Record(s): " + lvwAttendanceLogs.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       


        Dim byEmpName As String = "" + txtByEmpName.Text
        Dim byEmpNo As String = "" + txtByEmpNo.Text

        If byEmpName.Length <= 0 And byEmpNo.Length <= 0 Then
            Return
        End If


        Dim employeeIdArray As New List(Of String)

        If byEmpName.Length > 0 Then
            Dim empInfo As List(Of EmployeeInfo) = empInfoMgr.retrieveEmployeeInfoArrayByName(byEmpName)
            For Each info As EmployeeInfo In empInfo
                If Not employeeIdArray.Contains(info.employee_id.ToString) Then
                    employeeIdArray.Add(info.employee_id.ToString)
                End If
            Next
        End If

        If byEmpNo.Length > 0 Then
            Dim empInfo As List(Of EmployeeInfo) = empInfoMgr.retrieveEmployeeInfoArrayByEmployeeNo(byEmpNo)
            For Each info As EmployeeInfo In empInfo
                If Not employeeIdArray.Contains(info.employee_id.ToString) Then
                    employeeIdArray.Add(info.employee_id.ToString)
                End If
            Next
        End If


        Dim dataList As List(Of LogBiometrics) = logMgr.retrieveLogBioByEmployeeIDArray(employeeIdArray)

        populateListView(dataList)

    End Sub

    Private Sub btnAadminExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAadminExecute.Click
        Dim fromDate As String = dtpAdminDateFrom.Value.ToString("yyyy-M-d HH:mm:ss")
        Dim toDate As String = dtpAdminDateTo.Value.ToString("yyyy-M-d HH:mm:ss")

        Dim dataList As List(Of LogBiometrics) = logMgr.retrieveLogBioByDateOnly(fromDate, toDate)
        populateListView(dataList)
    End Sub
End Class